package lyh.hnu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import lyh.hnu.common.R;
import lyh.hnu.dto.DishDto;
import lyh.hnu.dto.SetmealDto;
import lyh.hnu.entity.po.Dish;
import lyh.hnu.entity.po.Setmeal;
import lyh.hnu.entity.po.SetmealDish;
import lyh.hnu.service.CategoryService;
import lyh.hnu.service.DishService;
import lyh.hnu.service.SetmealDishService;
import lyh.hnu.service.SetmealService;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        return setmealService.saveWithDish(setmealDto);
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageinfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name)
                .orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> p = setmealService.page(pageinfo, queryWrapper);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(p, setmealDtoPage, "records");
        List<Setmeal> setmealList = p.getRecords();
        List<SetmealDto> setmealDtoList = setmealList.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            setmealDto.setCategoryName(categoryService.getById(item.getCategoryId()).getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        return setmealService.removeWithDish(ids);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable long id) {
        return setmealService.selectWithDish(id);
    }

    @PostMapping("/status/0")
    public R<String> stopSale(@RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId, ids).set(Setmeal::getStatus, 0);
        setmealService.update(updateWrapper);
        return R.success("停售套餐成功");
    }

    @PostMapping("/status/1")
    public R<String> onSale(@RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId, ids).set(Setmeal::getStatus, 1);
        setmealService.update(updateWrapper);
        return R.success("起售套餐成功");
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        return setmealService.updateWithDish(setmealDto);
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        List<Setmeal> setmealList = setmealService.lambdaQuery()
                .eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId())
                .eq(Setmeal::getStatus, setmeal.getStatus())
                .orderByDesc(Setmeal::getUpdateTime)
                .list();
        return R.success(setmealList);
    }

    @GetMapping("/dish/{id}")
    public R<List<DishDto>> selectWithDetails(@PathVariable Long id) {
        List<SetmealDish> setmealDishList = setmealDishService.lambdaQuery().eq(SetmealDish::getSetmealId, id).list();
        List<DishDto> dishDtoList = setmealDishList.stream()
                .map(item -> {
                    DishDto dishDto = new DishDto();
                    dishDto.setCopies(item.getCopies());
                    Dish dish = dishService.lambdaQuery().eq(Dish::getId, item.getDishId()).one();
                    BeanUtils.copyProperties(dish, dishDto);
                    return dishDto;
                })
                .collect(Collectors.toList());
        return R.success(dishDtoList);
    }
}
